<template>
  <div
    v-if="visible"
    class="toolbar-menu"
    :style="{ width: `${width}px` }"
    :class="`menu-size${size}`"
  >
    <div v-if="variableList.length === 0">
      <div class="dis-flex menu-item">工作区无变量</div>
    </div>
    <div v-else>
      <div v-for="(item, index) in variableList" :key="index">
        <div
          class="dis-flex menu-item"
          @click="openVariable(item)"
        >
          <div class="menu-icon mt-10 ml-5 mr-5 icon-edit-variable" />
          {{ item.Name }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VariableList',
  props: {
    variableList: {
      type: Array,
      default: () => []
    },
    width: {
      type: Number,
      default: 110
    },
    size: {
      type: Number,
      default: 16
    }
  },
  data() {
    return {
      visible: false
    };
  },
  methods: {
    show() {
      this.visible = true;
    },
    hide() {
      this.visible = false;
    },
    openVariable(item) {
      const payload = {
        value: { data: item.value, show: item.show },
        id: item.id,
        label: item.Name
      };
      this.$store.dispatch('matlab/openVariable', payload);
    }
  }
};
</script>

<style lang="scss" scopde>
.toolbar-menu {
  background: #fafafa;
  border: 1px solid #a7a7a7;
}
.toolbar-menu-divider {
  width: 100%;
  border-bottom: 1px solid #a7a7a7;
}
.menu-text {
  font-size: 0.9em;
}
.menu-item {
  height: 36px;
  line-height: 36px;
  border: 1px solid transparent;

  &:hover {
    border: 1px solid #0099ff;
  }
}
.menu-size16 {
  font-size: 14px;

  .menu-icon {
    width: 16px;
    height: 16px;
  }
}
.menu-size24 {
  font-size: 16px;

  .menu-icon {
    width: 24px;
    height: 24px;
    background-repeat: no-repeat;
  }
}
</style>
